package com.ksyun.campus.dataserver.domain;


import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author sxt
 * @version 1.0
 * @description TODO
 * @date 2023/8/9 21:21
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString
public class DataServerInfo extends ServerInfo {


    private int capacity;


    private int usedCapacity;

    /**
     * 机架
     */
    private String rack;

    /**
     * 区域：北京、上海、天津
     */
    private String zone;
    /**
     * 添加该节点上保存的所有文件副本信息，以fileNameId作为key
     * @return 线程安全的ConcurrentHashMap（考虑到列表可能被多个事件集中回调，从而产生并发问题）
     */
    private Map<String, ReplicaData> allReplicaDataMap;

    public DataServerInfo(String ip, String port, String zone, String rack, int capacity, int usedCapacity) {
        super(ip, port, zone, rack);
        this.capacity = capacity;
        this.usedCapacity = usedCapacity;
        allReplicaDataMap = new ConcurrentHashMap<>();
    }


    public DataServerInfo(int capacity, int usedCapacity) {
        this.capacity = capacity;
        this.usedCapacity = usedCapacity;
    }

    /**
     * 可使用容量比率
     *
     * @return
     */
    public double freeCapacityRatio() {
        return 1 - (double) usedCapacity / capacity;
    }

}
